package com.shopping.service.receive;

import com.shopping.constant.ResponseCode;
import com.shopping.dao.ReceiveDao;
import com.shopping.pojo.Receive;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ReceiveServiceImpl implements ReceiveService {

    @Autowired
    ReceiveDao receiveDao;

    @Override
    public Map<String, Object> addReceiver(Receive receive) {
        Map<String, Object> map = new HashMap<>();
        List<Receive> list = new ArrayList<>();
        try{
            receiveDao.insert(receive);
            list = receiveDao.selectReceives(receive.getUserId());
            int size = list.size();
            int receiveId = list.get(size).getId();
            map.put("code",ResponseCode.REQUEST_SUCCESSED.getValue());
            map.put("id",receiveId);
        }catch (Exception e){
            map.put("code", ResponseCode.SERVER_ERROR.getValue());
        }
        return map;
    }

    @Override
    public Map<String, Object> deleteReceiver(int id) {
        Map<String, Object> map = new HashMap<>();
        try {
            Receive receive = receiveDao.selectReceive(id);
            if(receive == null){
                map.put("code", ResponseCode.ENTITY_NOT_EXISTS.getValue());
                return map;
            }
            receiveDao.delete(id);
            List<Receive> receives = receiveDao.selectReceives(receive.getUserId());
            map.put("code", ResponseCode.REQUEST_SUCCESSED.getValue());
            map.put("receivers", receives);
        }catch (Exception e){
            map.put("code", ResponseCode.SERVER_ERROR.getValue());
            e.printStackTrace();
        }
        return map;
    }

    @Override
    public Map<String, Object> showAllReceivers(int userId) {
        Map<String, Object> map = new HashMap<>();
        try {
            List<Receive> receives = receiveDao.selectReceives(userId);
            if(receives.size() == 0){
                map.put("code", ResponseCode.ENTITY_NOT_EXISTS.getValue());
                return map;
            }
            map.put("code", ResponseCode.REQUEST_SUCCESSED.getValue());
            map.put("receivers", receives);
        }catch (Exception e){
            map.put("code", ResponseCode.SERVER_ERROR.getValue());
            e.printStackTrace();
        }
        return map;
    }

    @Override
    public Map<String, Object> showOneReceiver(int id) {
        Map<String, Object> map = new HashMap<>();
        try {
            Receive receive = receiveDao.selectReceive(id);
            if(receive == null){
                map.put("code", ResponseCode.ENTITY_NOT_EXISTS.getValue());
                return map;
            }
            map.put("code", ResponseCode.REQUEST_SUCCESSED.getValue());
            map.put("receiver", receive);
        }catch (Exception e){
            map.put("code", ResponseCode.SERVER_ERROR.getValue());
            e.printStackTrace();
        }
        return map;
    }

    @Override
    public Map<String, Object> modifyReceiver(Receive receive) {
        Map<String, Object> map = new HashMap<>();
        try {
            receiveDao.updateReceiverSelective(receive);
            Receive newReceive = receiveDao.selectReceive(receive.getId());
            map.put("code", ResponseCode.REQUEST_SUCCESSED.getValue());
            map.put("receiver", newReceive);
        }catch (Exception e){
            map.put("code", ResponseCode.SERVER_ERROR.getValue());
            e.printStackTrace();
        }
        return map;
    }

    @Override
    public Map<String, Object> selectUserIdById(int receiveId) {
        Map<String, Object> map = new HashMap<>();
        try {
            int userId = receiveDao.selectUserId(receiveId);
            map.put("code", ResponseCode.REQUEST_SUCCESSED.getValue());
            map.put("userId", userId);
        }catch (Exception e){
            map.put("code", ResponseCode.SERVER_ERROR.getValue());
            e.printStackTrace();
        }
        return map;
    }

}
